//
//  TTTableView.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2018/12/29.
//  Copyright © 2018 Gox_WallyDiao. All rights reserved.
//

import UIKit

class TTTableView: UITableView {
    typealias PullBlock = ((_ tableView:UITableView)->())
    var pullFooter:PullBlock?
    var pullHeader:PullBlock?
    var noDataView:UIView?
    var imageView:UIImageView?
    var imageLabel:UILabel?
    
//    var backImage:UIImageView = {
//        let image = UIImageView.init(frame: FRAME_CGFloat(SCREEN_WIDTH / 2 - 81, 60, 162, 228))
//        image.image = IMAGE(name: "Mine_head")
//        return image
//    }()
//
//    var backLabel:UILabel = {
//        let label = UILabel.init(frame: FRAME_CGFloat(0, 300, SCREEN_WIDTH, 22.5))
//        label.text = "小编正在努力搬运上传哦~"
//        label.font = FONT(16)
//        label.textColor = RGB_Same(53)
//        label.textAlignment = .center
//        return label
//    }()
    
    
//    var noDataView:NoDataView =
    /*
     // Only override draw() if you perform custom drawing.
     // An empty implementation adversely affects performance during animation.
     override func draw(_ rect: CGRect) {
     // Drawing code
     }
     */
    
    override init(frame: CGRect, style: UITableView.Style) {
        super.init(frame: frame, style: style)
        noDataView = UIView.init(frame: FRAME_CGFloat(0, 0, FRAME_W(frame: frame), FRAME_H(frame: frame)))
        self.addSubview(noDataView!)
        setUI()
        self.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        setNoData(nodata: false)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
//        noDataView = UIView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, 400))
        self.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
//        self.addSubview(noDataView!)
//        setUI()
//        setNoData(nodata: false)
    }
    
    
    //  MARK: - ###### 产品寿命 ######
    
    
    //  MARK: - ###### 子视图 ######
    
    //  MARK: - ###### 变量 ######
    
}

//  MARK: - ###### 协议 ######
extension TTTableView {}

//  MARK: - ###### 事件处理 ######
extension TTTableView {}

//  MARK: - ###### 私有方法 ######
extension TTTableView {
    func setUI() {
        imageView = UIImageView.init(frame: FRAME_CGFloat(SCREEN_WIDTH / 2 - 81, 60, 162, 228))
        imageView?.image = IMAGE(name: "placeholderImage")
        noDataView!.addSubview(imageView!)
        
        imageLabel = UILabel.init(frame: FRAME_CGFloat(0, 300, SCREEN_WIDTH, 20))
        imageLabel?.text = "小编正在努力搬运上传哦~"
        imageLabel?.font = Font_Semibold(14)
        imageLabel?.textColor = RGB_Same(178)
        imageLabel?.textAlignment = .center
        noDataView!.addSubview(imageLabel!)
    }
    
    func hideExtraCellLine() {
        let view = UIView()
        view.backgroundColor = UIColor.clear
        self.tableFooterView = view
    }
    
}

//  MARK: - ###### 公有方法 ######
extension TTTableView {
    
    func initMjFooterWithOffest(_ offest:Float , _ back: @escaping (_ tableView:UITableView) -> ()) {
        pullFooter = back
        self.mj_footer = MJRefreshBackNormalFooter(refreshingTarget: self, refreshingAction:#selector(upPullLoadData))
        
    }
    
    func initMjHeaderWithOffest(_ offest:Float , _ back: @escaping (_ tableView:UITableView) -> ()) {
        pullHeader = back
        self.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction:#selector(downPullLoadData))
//        self.mj_header.headOffset = CGFloat(offest)
        
    }
    
    //  MARK: ***** 加载数据
    @objc func upPullLoadData(){
        self.mj_header?.endRefreshing()
        if self.pullFooter != nil {
            self.pullFooter!(self)
        }
    }
    
    @objc func downPullLoadData(){
        self.mj_footer?.endRefreshing()
        if self.pullHeader != nil {
            self.pullHeader!(self)
        }
    }
    
    //  MARK: *****
    func endXWHeadRefreshing()  {
        self.mj_header?.endRefreshing()
    }
    func endXWFooterRefreshing()  {
        self.mj_footer?.endRefreshing()
    }
    func endAllRefreshing()  {
        endXWFooterRefreshing()
        endXWHeadRefreshing()
    }
    
    func setFooterNoMore()  {
        self.mj_footer.endRefreshingWithNoMoreData()
    }
    
    func resetFooterNoMore()  {
        self.mj_footer.resetNoMoreData()
    }
    
    //  MARK: *****begin
    func beginHeadRefresh()  {
        if self.mj_header != nil && !self.mj_header.isRefreshing {
            self.mj_header?.beginRefreshing()
        }
    }
    func beginFooterRefresh()  {
        if self.mj_footer != nil && !self.mj_footer.isRefreshing {
            self.mj_footer?.beginRefreshing()
        }
    }
    
    func setNoData(nodata:Bool)  {
        noDataView!.frame = CGRect.init(x: 0, y: 0, width: VIEW_WIDTH(view: self), height: VIEW_HEIGHT(view: self))
        
        noDataView!.isHidden = !nodata
    }
    
    func setNoData(nodata:Bool, offY:CGFloat)  {
        noDataView!.frame = CGRect.init(x: 0, y: 0, width: VIEW_WIDTH(view: self), height: VIEW_HEIGHT(view: self))
        imageView?.frame = FRAME_CGFloat(SCREEN_WIDTH / 2 - 81, offY, 162, 228)
        imageLabel?.frame = FRAME_CGFloat(0, offY + 240, SCREEN_WIDTH, 20)
        noDataView!.isHidden = !nodata
    }
    
    func setNoData(nodata:Bool,frame:CGRect, image:String, des:String)  {
        noDataView!.frame = CGRect.init(x: 0, y: 0, width: VIEW_WIDTH(view: self), height: VIEW_HEIGHT(view: self))
        imageView?.frame = frame
        imageView?.image = IMAGE(name: image)
        imageLabel?.text = des
        noDataView!.isHidden = !nodata
    }
    
    
}

